package com.eedi.org.hospital.service;

import com.eedi.framework.common.enums.CommonStatusEnum;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.common.util.object.BeanUtils;
import com.eedi.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.eedi.org.hospital.controller.vo.hosp.OrgHospitalPageReqVO;
import com.eedi.org.hospital.controller.vo.hosp.OrgHospitalSaveReqVO;
import com.eedi.org.hospital.dal.dataobject.OrgHospitalDO;
import com.eedi.org.hospital.dal.mapper.OrgHospitalMapper;
import com.eedi.org.hospital.facade.OrgDeptFacade;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

import static com.eedi.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.eedi.org.enums.OrgErrorCodeConstants.*;


/**
 * 医院 Service 实现类
 *
 * @author 永聚长青源码
 */
@Service
@Validated
public class OrgHospitalServiceImpl implements OrgHospitalService {

    @Resource
    private OrgHospitalMapper orgHospitalMapper;

    @Resource
    private OrgDeptFacade orgDeptFacade;

    @Override
    public String createHospital(OrgHospitalSaveReqVO createReqVO) {
        //根据名称判重，不允许同名插入
        if (orgHospitalMapper.selectByName(createReqVO.getOrgHospName()) != null) {
            throw exception(HOSPITAL_SAME_NAME_EXIST);
        }
        // 插入
        OrgHospitalDO hospital = BeanUtils.toBean(createReqVO, OrgHospitalDO.class);
        orgHospitalMapper.insert(hospital);
        // 返回
        return hospital.getOrgHospitalId();
    }

    @Override
    public void updateHospital(OrgHospitalSaveReqVO updateReqVO) {
        // 校验存在
        validateHospitalExists(updateReqVO.getOrgHospitalId());
        //根据名称判重，不允许同名插入
        OrgHospitalDO existHospital = orgHospitalMapper.selectByName(updateReqVO.getOrgHospName());
        if (existHospital != null && !existHospital.getOrgHospitalId().equals(updateReqVO.getOrgHospitalId())) {
            throw exception(HOSPITAL_SAME_NAME_EXIST);
        }
        // 更新
        OrgHospitalDO updateObj = BeanUtils.toBean(updateReqVO, OrgHospitalDO.class);
        orgHospitalMapper.updateById(updateObj);
    }

    @Override
    public void deleteHospital(String orgHospitalId) {
        // 校验存在
        validateHospitalExists(orgHospitalId);
        //校验医院下是否有医生存在
        if (orgDeptFacade.validExistDept(orgHospitalId, null)) {
            throw exception(HOSPITAL_DOCTOR_EXIST_AVOID_DEPT);
        }
        orgHospitalMapper.deleteById(orgHospitalId);
    }

    private void validateHospitalExists(String orgHospitalId) {
        if (orgHospitalMapper.selectById(orgHospitalId) == null) {
            throw exception(HOSPITAL_NOT_EXISTS);
        }
    }

    @Override
    public OrgHospitalDO getHospital(String orgHospitalId) {
        return orgHospitalMapper.selectById(orgHospitalId);
    }

    @Override
    public void updateHospitalStatus(String orgHospitalId, CommonStatusEnum status) {
        // 校验存在
        validateHospitalExists(orgHospitalId);

        // 更新状态
        OrgHospitalDO updateObj = new OrgHospitalDO();
        updateObj.setOrgHospitalId(orgHospitalId);
        updateObj.setOrgStatus(status);
        orgHospitalMapper.updateById(updateObj);
    }

    @Override
    public List<OrgHospitalDO> listHospital() {
        return orgHospitalMapper.selectList(new LambdaQueryWrapperX<OrgHospitalDO>()
                .eqIfPresent(OrgHospitalDO::getOrgStatus, CommonStatusEnum.ENABLE));
    }

    @Override
    public PageResult<OrgHospitalDO> getHospitalPage(OrgHospitalPageReqVO pageReqVO) {
        return orgHospitalMapper.selectPage(pageReqVO);
    }

    @Override
    public List<OrgHospitalDO> listByIds(List<String> orgHospitalIds) {
        if (CollectionUtils.isEmpty(orgHospitalIds)) {
            return Collections.emptyList();
        }
        return orgHospitalMapper.selectBatchIds(orgHospitalIds);
    }

}
